<?php

namespace app\common\model;

use think\Model;
use think\Db;
class Admin extends Model
{
    //
    public function addData($data){
        $data = $this->checkData($data);
        if(isset($data['msg'])) return $data;
        $where['phone'] = $data['phone'];
        $where['email'] = $data['email'];
        $where = (isset($data['id']) ? db('admin')->where(function($query)use($data){
          $query->where('id','<>', $data['id']);
        })->where(function ($query)use($data,$where){
            $query->where(['number'=>$data['number']])->whereOR($where);
        })->find() : db('admin')->where(['number'=>$data['number']])->whereOR($where)->find());
        if($where['number'] == $data['number']) return ['status'=>0, 'msg'=>'编号已存在!'];
        if($where['phone'] == $data['phone']) return ['status'=>0, 'msg'=>'手机号码已存在!'];
        if($where['email'] == $data['email']) return ['status'=>0, 'msg'=>'邮箱已存在!'];
        Db::startTrans(); // 开启事务
        if(isset($data['id'])){
            $id = array_shift($data);
            $rid = array_splice($data,'1',1)['rid'];
            $update = db('admin')->where('id',$id)->setField($data);
            if($update !== false){
                if((db('role_admin')->where('aid',$id)->setField(['rid'=>$rid])) !== false){
                    Db::commit(); // 提交事务
                    return ['status'=>1, 'msg'=>'已更新!'];
                }
                Db::rollback(); // 回滚事务
                return ['status'=>0, 'msg'=>'更新关联表失败!'];
            }
            return ['status'=>0, 'msg'=>'更新失败!'];
        }
        $rid = array_splice($data,'1',1)['rid'];
        $aid = db('admin')->insertGetId($data);
        if($aid){
            if(db('role_admin')->insert(['rid'=>$rid,'aid'=>$aid])){
                Db::commit(); // 提交事务
                return ['status'=>1, 'msg'=>'已添加!'];
            }
            Db::rollback(); // 回滚事务
            return ['status'=>0, 'msg'=>'添加失败!'];
        }
        return ['status'=>0, 'msg'=>'添加失败!'];

    }
    public function checkData($data){
        if(!$data['name']) return ['status'=>0, 'msg'=>'姓名不能为空!'];
        if(!$data['rid']) return ['status'=>0, 'msg'=>'角色不能为空!'];
        if(!$data['number']) return ['status'=>0, 'msg'=>'编号不能为空!'];
        if(!$data['phone']) return ['status'=>0, 'msg'=>'手机号码不能为空!'];
        preg_match(config('MOBILE_PREG_MATCH'),$data['phone'],$match);
        if(!$match) return ['status'=>0, 'msg'=>'手机号码错误!'];
        if(!$data['email']) return ['status'=>0, 'msg'=>'邮箱不能为空!'];
        preg_match(config('EMAIL_PREG_MATCH'),$data['email'],$matchs);
        if(!$matchs) return ['status'=>0, 'msg'=>'邮箱格式错误!'];
        $data['password'] = password_hash(substr($data['phone'], -6, 6), PASSWORD_DEFAULT);
        isset($data['id']) || $data['addTime'] = time();
        return $data;
    }
    public function del($ids)
    {
        $ids = trim($ids,',');
        Db::startTrans(); // 开启事务
        $data = db('admin')->delete($ids);
        if($data){
            if(db('role_admin')->where(['aid'=>['in',$ids]])->delete()){
                Db::commit(); // 提交事务
                return ['status'=>1,'msg'=>'已删除!'];
            }
            Db::rollback(); // 回滚事务
            return ['status'=>0,'msg'=>'删除失败!'];
        }
        return ['status'=>0,'msg'=>'删除失败!'];
    }
}
